<template>
    <div class="app-container">
        <div class="head-container">
            <!-- 搜索 -->
            <table>
                <tr>
                    <td>关联项目:</td>
                    <td>
                        <el-input v-model="listQuery.markedness" clearable size="mini" style="width: 200px;"
                            class="filter-item" @keyup.enter.native="handleFilter" />
                    </td>
                    <td>
                        单据主题:<el-input v-model="listQuery.theme" clearable size="mini" style="width: 200px;"
                            class="filter-item" @keyup.enter.native="handleFilter" />
                    </td>
                    <td>单据编码:</td>
                    <td>
                        <el-input v-model="listQuery.serialNumber" clearable size="mini" style="width: 200px;"
                            class="filter-item" @keyup.enter.native="handleFilter" />
                    </td>
                    <td>添加人员:</td>
                    <td>
                        <el-input v-model="listQuery.pricing" clearable size="mini" style="width: 200px;"
                            class="filter-item" @keyup.enter.native="handleFilter" />
                    </td>
                </tr>
                <tr>
                    <td>日期:</td>
                    <td>
                        <el-date-picker type="date" placeholder="开始日期" v-model="listQuery.startDate"
                            style="width: 184px;">
                        </el-date-picker>

                    </td>
                    <td>
                        <el-date-picker type="date" placeholder="结束日期" v-model="listQuery.endDate"
                            style="width: 184px;">
                        </el-date-picker>
                    </td>
                    <el-button class="filter-item" size="mini" type="success" icon="el-icon-search"
                        @click="handleFilter">搜索
                    </el-button>
                </tr>
            </table>
        </div>
        <!-- 列表显示 -->
        <el-table ref="multipleTable" v-loading="listLoading" :data="list" size="small" style="width: 90%;"
            @sort-change="sortChange" @selection-change="handleSelectionChange" @row-click="handleRowClick">
            <el-table-column type="selection" width="44px"></el-table-column>


            <el-table-column label="采购询价编号" prop="serialNumber" align="center" />
            <el-table-column label="采购询价主题" prop="theme" align="center" />

            <el-table-column label="产品名称" prop="getProduceDtoList[0].proName" align="center" />
            <el-table-column label="产品编号" prop="getProduceDtoList[0].proCode" align="center" />
            <el-table-column label="规格型号" prop="getProduceDtoList[0].specType" align="center" />
            <el-table-column label="单位" prop="getProduceDtoList[0].unit" align="center" />
            <el-table-column label="询价日期" prop="enquiry" align="center" />
            <el-table-column label="定价人员" prop="pricing" align="center" />
            <el-table-column label="询价人员" prop="personnel" align="center" />
            <el-table-column label="关联项目" prop="markedness" align="center" />
            <el-table-column label="来源单据类型" prop="receiptsType" align="center" />


            <el-table-column label="操作" align="center">
                <template slot-scope="{row}">
                    <el-button type="primary" size="mini" @click="handleUpdate(row)" icon="el-icon-edit" />
                    <el-button type="danger" size="mini" @click="handleDelete(row)" icon="el-icon-delete" />
                </template>
            </el-table-column>



        </el-table>
        <pagination v-show="totalCount > 0" :total="totalCount" :page.sync="page" :limit.sync="listQuery.MaxResultCount"
            @pagination="getList" />
    </div>
</template>
<script>
import Pagination from "@/components/Pagination";
import permission from "@/directive/permission/index.js";
import { cloneDeep } from "lodash";

//添加数据
const defaultForm = {
    "receiptsType": "",
    "source": "",
    "theme": "",
    "serialNumber": "",
    "pricing": "",
    "enquiry": "",
    "personnel": "",
    "markedness": "",
    "remark": ""
}



export default {
    data() {
        return {};
    },
    name: 'Book',
    components: {
        Pagination
    },
    directives: {
        permission
    },
    props: [],
    data() {
        return {
            form: Object.assign({}, defaultForm),
            list: [],
            totalCount: 0,
            listLoading: true,
            formLoading: false,
            listQuery: {
                theme: '',
                serialNumber: "",
                pricing: '',
                Sorting: '',
                markedness: '',
                SkipCount: 0,
                MaxResultCount: 10,
                EnquiryDate: '',
                startDate: '',
                endDate: ''
            },
            page: 1,
            dialogFormVisible: false,
            multipleSelection: [],
            formTitle: '',
            isEdit: false,
        }
    },
    computed: {},
    watch: {},
    created() {
        this.getList()
    },
    mounted() { },
    methods: {


        //显示方法
        getList() {
            //this.listLoading = true;
            this.listQuery.SkipCount = (this.page - 1) * this.listQuery.MaxResultCount;
            this.$axios.gets('/api/business/purch/getAlls', this.listQuery).then(response => {
                this.totalCount = response.totalCount;
                this.list = response.items
                this.listLoading = false;
                //this.getListStatus(response.items, response.items.map(_ => _.sid))
            });
        },
        fetchData(id) {
            this.$axios.gets('/api/business/purch/getAlls' + id).then(response => {
                this.form = response;
            });
        },
        handleFilter() {
            this.page = 1;
            this.getList();
        },
        handleCreate() {
            this.formTitle = '采购文件';
            this.isEdit = false;
            this.dialogFormVisible = true;
        },
        handleDelete(row) {
            var params = [];
            let alert = '';
            if (row) {
                params.push(row.id);
                alert = row.sname;
            }
            else {
                if (this.multipleSelection.length === 0) {
                    this.$message({
                        message: '未选择',
                        type: 'warning'
                    });
                    return;
                }
                this.multipleSelection.forEach(element => {
                    let id = element.id;
                    params.push(id);
                    console.log(params);
                });

                console.log(params);
                alert = '选中项';
            }
            this.$confirm('是否删除' + alert + '?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$axios.posts('/api/business/purch/delete', params).then(response => {
                    this.$notify({
                        title: '成功',
                        message: '删除成功',
                        type: 'success',
                        duration: 2000
                    });
                    this.getList();
                });
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        handleCheck() {
            if (this.multipleSelection.length === 0) {
                this.$message({
                    message: '未选择',
                    type: 'warning'
                });
                return;
            }
            if (this.multipleSelection.length > 1) {
                this.$message({
                    message: '暂不支持批量审核',
                    type: 'warning'
                });
                return;
            }
            this.$confirm('是否审核所选项?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$axios.puts('api/business/workflow/do/' + this.multipleSelection[0].id, { data: JSON.stringify(this.multipleSelection[0]) }).then(response => {
                    this.$notify({
                        title: '成功',
                        message: '审核成功',
                        type: 'success',
                        duration: 2000
                    });
                    this.getList();
                });
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消审核'
                });
            });
        },
        findFlow(row) {
            this.$router.push({ path: "/tool/flowDisplay/" + row.nodeId });
        },
        handleUpdate(row) {
            this.formTitle = '修改图书';
            this.isEdit = true;
            if (row) {
                this.fetchData(row.id);
                this.dialogFormVisible = true;
            }
            else {
                if (this.multipleSelection.length != 1) {
                    this.$message({
                        message: '编辑必须选择单行',
                        type: 'warning'
                    });
                    return;
                }
                else {
                    this.fetchData(this.multipleSelection[0].id);
                    this.dialogFormVisible = true;
                }
            }
        },
        save() {
            this.$refs.form.validate(valid => {
                if (valid) {
                    this.formLoading = true;
                    this.form.roleNames = this.checkedRole;
                    if (this.isEdit) {
                        this.$axios.posts('/api/business/purch/data-post', this.form).then(response => {
                            this.formLoading = false;
                            this.$notify({
                                title: '成功',
                                message: '更新成功',
                                type: 'success',
                                duration: 2000
                            });
                            this.dialogFormVisible = false;
                            this.getList();
                        }).catch(() => {
                            this.formLoading = false;
                        });
                    }
                    else {
                        this.$axios.posts('/api/business/purch/data-post', this.form).then(response => {
                            this.formLoading = false;
                            this.$notify({
                                title: '成功',
                                message: '新增成功',
                                type: 'success',
                                duration: 2000
                            });
                            this.dialogFormVisible = false;
                            this.getList();
                        }).catch(() => {
                            this.formLoading = false;
                        });
                    }
                }
            });

        },
        sortChange(data) {
            const {
                prop,
                order
            } = data;
            if (!prop || !order) {
                this.handleFilter();
                return;
            }
            this.listQuery.Sorting = prop + ' ' + order;
            this.handleFilter();
        },
        handleSelectionChange(val) {
            this.multipleSelection = val;
            console.log(val);
        },
        handleRowClick(row, column, event) {
            this.$refs.multipleTable.clearSelection();
            this.$refs.multipleTable.toggleRowSelection(row);
        },
        cancel() {
            this.form = Object.assign({}, defaultForm);
            this.dialogFormVisible = false;
            this.$refs.form.clearValidate();
        },
    }
}

</script>
<style></style>

</script>
<style></style>
